//
//  EditViewController.m
//  book
//  1.plist存储数据，Preference存储数据（本质都是XML），都不能存储对象类型
//  2.SQLite也不能存储对象类型，CoreData可以存储对象类型
//  3.
//
//
//
//  Created by cocoazxj@163.com on 15/4/7.
//  Copyright (c) 2015年 handcool. All rights reserved.
//

#import "EditViewController.h"
#import "ContactModel.h"

@interface EditViewController ()
@property (nonatomic, strong) IBOutlet UIButton *btn_save;
@property (nonatomic, strong) IBOutlet UITextField *tf_name;
@property (nonatomic, strong) IBOutlet UITextField *tf_phoneNum;
@end

@implementation EditViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.tf_name.text = self.contactModel.name;
    self.tf_phoneNum.text = self.contactModel.phoneNum;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)saveButtonClicked:(id)sender
{
    self.contactModel.name = self.tf_name.text;
    self.contactModel.phoneNum = self.tf_phoneNum.text;
    if([self.delegate respondsToSelector:@selector(editViewControllerDelegate:contactModel:)])
    {
        [self.delegate editViewControllerDelegate:self contactModel:self.contactModel];
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)editButtonClicked:(UIBarButtonItem *)sender
{
    // 可编辑
    if(self.tf_name.enabled && self.tf_phoneNum.enabled)
    {
        self.tf_name.enabled = NO;
        self.tf_phoneNum.enabled = NO;
        self.btn_save.hidden = YES;
        self.tf_phoneNum.text = self.contactModel.phoneNum;
        sender.title = @"编辑";
    }
    else
    {
        self.tf_name.enabled = YES;
        self.tf_phoneNum.enabled = YES;
        [self.tf_phoneNum becomeFirstResponder];
        self.btn_save.hidden = NO;
        sender.title = @"取消";
    }
}
@end
